import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./plugins/element.js";
import "./assets/css/comm.scss";
import Cookies from "js-cookie";
Vue.config.productionTip = false;
router.beforeEach((to, from, next) => {
  /*
   * 判断一下当前是否需要登陆
   * */
  const {
    meta: { requiresAuth }
  } = to;
  if (requiresAuth) {
    const token = Cookies.get("access-token");
    const username = Cookies.get("username");
    if (token && username) {
      next();
    } else {
      next({
        path: "/",
        query: { redirect: to.name } //将跳转的路由path作为参数，登录成功后跳转到进页面之前的路由地址，保证和登陆后获取的值一致
      });
    }
  } else {
    next();
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
